<?php
/**
 * Created by PhpStorm.
 * User: zhengkunming
 * Date: 2018/12/26
 * Time: 3:57 PM
 */

require_once 'mysql_config.php';

$openid = isset($_POST['openid']) ? $_POST['openid'] : '';
$full_name = isset($_POST['full_name']) ? $_POST['full_name'] : '';
$mobile = isset($_POST['mobile']) ? $_POST['mobile'] : '';
$shop = isset($_POST['shop']) ? $_POST['shop'] : '';

$score = 100;

if ('' === $openid) {
    $data = array(
        'code' => -1,
        'data' => array(),
        'msg' => 'require openid'
    );
    rtJson($data);
    return;
}

if ('' === $full_name) {
    $data = array(
        'code' => -1,
        'data' => array(),
        'msg' => 'require full name'
    );
    rtJson($data);
    return;
}

if ('' === $mobile) {
    $data = array(
        'code' => -1,
        'data' => array(),
        'msg' => 'require mobile'
    );
    rtJson($data);
    return;
}

if ('' === $shop) {
    $data = array(
        'code' => -1,
        'data' => array(),
        'msg' => 'require shop'
    );
    rtJson($data);
    return;
}

$query_mobile = 'SELECT * FROM users WHERE users.mobile = "' . $mobile . '";';

//echo $query_mobile ;

$query_mobile_res = mysqli_query($link, $query_mobile);

if (1 === $query_mobile_res->num_rows) {
    $data = array(
        'code' => -3,
        'data' => array(),
        'msg' => '手机号已存在'
    );
    rtJson($data);
    return;
}

//return;

$query = 'INSERT INTO users (openid, full_name, mobile, shop, score, created) VALUE ("' . $openid . '","' . $full_name . '","' . $mobile . '","' . $shop . '",' . $score . ',"' . $current_time . '")';

$query_res = mysqli_query($link, $query);

if (TRUE === $query_res) {
    $data = array(
        'code' => 0,
        'data' => array(),
        'msg' => 'update success'
    );
    rtJson($data);
    return;
} else {
    $data = array(
        'code' => -2,
        'data' => array(),
        'msg' => 'update fail'
    );
    rtJson($data);
    return;
}
